package org.dromara.third.convert.troubleShootingRecord.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.TroubleShootingRecord;
import org.dromara.system.mapper.TroubleShootingRecordMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.troubleShootingRecord.dto.TroubleShootingRecordDTO;
import org.dromara.third.convert.troubleShootingRecord.validator.TroubleShootingRecordValidactor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertTroubleShootingRecordController {

    @Autowired
    private TroubleShootingRecordMapper troubleShootingRecordMapper;

    @PostMapping("/troubleShootingRecord")
    @Transactional
    public R<Void> addTroubleShootingRecord(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        TroubleShootingRecordDTO troubleShootingRecordDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            troubleShootingRecordDTO = JSONObject.parseObject(decrypt, TroubleShootingRecordDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, troubleShootingRecordDTO, new TroubleShootingRecordValidactor());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        TroubleShootingRecord troubleShootingRecord = assembleTroubleShootingRecordData(orgCode, troubleShootingRecordDTO);
        int count = troubleShootingRecordMapper.countWithOutDelFlag(troubleShootingRecord.getId());
        if (count == 0) {
            troubleShootingRecordMapper.insert(troubleShootingRecord);
        } else {
            troubleShootingRecordMapper.updateWithOutDelFlag(troubleShootingRecord);
        }
        return R.ok();
    }

    private TroubleShootingRecord assembleTroubleShootingRecordData(String orgCode, TroubleShootingRecordDTO dto) {
        TroubleShootingRecord troubleShootingRecord = new TroubleShootingRecord();
        try {
            troubleShootingRecord.setId(dto.getId());
            troubleShootingRecord.setCheckTaskId(dto.getCheckTaskId());
            troubleShootingRecord.setCheckTime(DateUtils.parseDate(dto.getCheckTime()));
            troubleShootingRecord.setMobileMe(dto.getMobileMe());
            troubleShootingRecord.setIsDefend(dto.getIsDefend());
            troubleShootingRecord.setCheckStatus(dto.getCheckStatus());
            troubleShootingRecord.setCheckPerson(dto.getCheckPerson());
            troubleShootingRecord.setCheckPersonTel(dto.getCheckPersonTel());
            troubleShootingRecord.setUndetectedFlag(dto.getUndetectedFlag());
            troubleShootingRecord.setDelFlag(Long.parseLong(dto.getDelFlag()));
            troubleShootingRecord.setCreateDept(Long.parseLong(dto.getCreateDept()));
            troubleShootingRecord.setCreateBy(Long.parseLong(dto.getCreateBy()));
            troubleShootingRecord.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            troubleShootingRecord.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            troubleShootingRecord.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            troubleShootingRecord.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装隐患排查记录信息异常");
        }
        return troubleShootingRecord;
    }
}
